package com.macro.mall.portal.controller;

import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.SmsMessageHistory;
import com.macro.mall.portal.domain.SmsMessageHistoryQueryParam;
import com.macro.mall.portal.service.SmsMessageHistoryService;
import com.macro.mall.portal.sms.SmsMessageService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信Controller
 *
 * @author 郎
 */
@Controller
@Api(tags = "SmsController")
@Tag(name = "SmsController", description = "发送短信业务")
@RequestMapping("/sms")
@AllArgsConstructor
public class SmsController {

    private SmsMessageHistoryService messageHistoryService;
    private SmsMessageService smsMessageService;

    @AccessLimit
    @ApiOperation("根据短信id获取短信详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SmsMessageHistory> getMessageHistoryInfo(@PathVariable Long id) {
        return CommonResult.success(messageHistoryService.getMessageHistoryInfo(id));
    }

    @AccessLimit
    @ApiOperation("查询短信列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SmsMessageHistory>> getList(SmsMessageHistoryQueryParam queryParam,
                                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsMessageHistory> messageHistoryList = messageHistoryService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(messageHistoryList));
    }

    @AccessLimit
    @ApiOperation("发送短信验证码")
    @RequestMapping(value = "/sendMessageVerifyCode", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> sendMessageVerifyCode(@RequestParam("phone") String phone, @RequestParam("verifyCode") String verifyCode) throws TencentCloudSDKException {
        boolean success = smsMessageService.sendMessage(phone, verifyCode);
        return success ? CommonResult.success("发送成功") : CommonResult.failed();
    }
}
